<%--
    author: Ivan Holub
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Clear Streets</title>
    <script type="text/javascript" src="/js/prototype-1.6.0.3.js"></script>
    <script type="text/javascript">
        var isRunning = false;
        function onStart() {
            hideError();
            $('btnStart').disabled = true;
            $('btnStop').disabled = false;
            isRunning = true;
            process();
        }
        function hideError() {
            $('errormessage').hide();
        }
        function showError(error) {
            if (error) {
                $('errormessage').innerHTML = error;
            } else {
                $('errormessage').innerHTML = 'Error occured';
            }
            $('errormessage').show();
        }
        function process() {
            if (!isRunning) {
                return;
            }
            var number = $('number').value;
            new Ajax.Request('/admin/osm/streets/clear/service.do',
            {
                method:'post',
                parameters: {
                    number: number
                },
                onSuccess: function(transport) {
                    try {
                        var response = transport.responseText;
                        if (!response) {
                            showError();
                            return;
                        }
                        var data = response.evalJSON();
                        var maxNumber = data.maxNumber;
                        $("maxNumber").innerHTML = maxNumber;
                        if (maxNumber > 0) {
                            process();
                        } else {
                            onStop();
                        }
                    } catch (e) {
                        onStop();
                        showError(e);
                    }
                },
                onFailure: function() {
                    onStop();
                    showError();
                }
            });
        }
        function onStop() {
            isRunning = false;
            $('btnStart').disabled = false;
            $('btnStop').disabled = true;
        }
    </script>
</head>

<body>

<h3>Ajax Clear Streets</h3>

<p>
    Max number: <span id="maxNumber"><c:out value="${maxNumber}"/></span>
</p>

<div id="errormessage" style="display:none;"></div>

Number: <input type="text" id="number" value="100"/>
<input type="button" id="btnStart" value="Clear" onclick="onStart();"/>
<input type="button" id="btnStop" value="Stop" onclick="onStop();" disabled="true"/>

</body>
</html>
